﻿using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FAMailUtils.MailGun
{
    public class CreateResult
    {
        public string message { get; set; }
        public RoutItem route { get; set; }
        public string domain { get; set; }
    }
    public class ListResult
    {
        public int total_count { get; set; }
        public List<RoutItem> items { get; set; }
    }
    public class RoutItem
    {
        public string description { get; set; }
        public DateTime created_at { get; set; }
        public List<string> actions { get; set; }
        public string priority { get; set; }
        public string expression { get; set; }
        public string id { get; set; }
    }
    public class Routes
    {
        public static CreateResult CreateRoute(string apiKey, string forwardUrl, string domain)
        {
            //ListResult routes = GetRoutes(apiKey);
            CreateResult result = new CreateResult();
            //int i = routes.items.Select(a => a.expression.IndexOf("('.*@" + domain+"')")>=0).Count();
            //if (i == 0)
           // {
                RestClient client = new RestClient();
                client.BaseUrl = "https://api.mailgun.net/v2";
                client.Authenticator =
                        new HttpBasicAuthenticator("api",
                                                   apiKey);
                RestRequest request = new RestRequest();
                request.Resource = "routes";
                request.AddParameter("priority", 0);
                request.AddParameter("description", domain);
                request.AddParameter("expression",
                                     "match_recipient('.*@" + domain + "')");
                request.AddParameter("action",
                                     "forward('" + forwardUrl + "')");//http://famails.com/tracking/inbound.aspx
                request.AddParameter("action", "stop()");
                request.Method = Method.POST;
                IRestResponse response = client.Execute(request);
                result = Newtonsoft.Json.JsonConvert.DeserializeObject<CreateResult>(response.Content);
                result.domain = domain;
           // }
            return result;
        }

        public static ListResult GetRoutes(string apiKey)
        {
            RestClient client = new RestClient();
            client.BaseUrl = "https://api.mailgun.net/v2";
            client.Authenticator =
                    new HttpBasicAuthenticator("api",
                                               apiKey);
            RestRequest request = new RestRequest();
            request.Resource = "routes";
            //request.AddParameter("skip", 1);
            //request.AddParameter("limit", 100);
            IRestResponse result = client.Execute(request);

            ListResult items = new ListResult();
            items = Newtonsoft.Json.JsonConvert.DeserializeObject<ListResult>(result.Content);
            return items;
        }
        public static CreateResult GetRouteByID(string apiKey, string id)
        {
            RestClient client = new RestClient();
            client.BaseUrl = "https://api.mailgun.net/v2";
            client.Authenticator =
                    new HttpBasicAuthenticator("api",
                                               apiKey);
            RestRequest request = new RestRequest();
            request.Resource = "routes/{id}";
            request.AddUrlSegment("id", id);
            IRestResponse result = client.Execute(request);
            return Newtonsoft.Json.JsonConvert.DeserializeObject<CreateResult>(result.Content);
        }
        public static void DeleteRouteByID(string apiKey, string id)
        {
            RestClient client = new RestClient();
            client.BaseUrl = "https://api.mailgun.net/v2";
            client.Authenticator =
                    new HttpBasicAuthenticator("api",
                                               apiKey);
            RestRequest request = new RestRequest();
            request.Resource = "routes/{id}";
            request.AddUrlSegment("id", id);
            request.Method = Method.DELETE;
            IRestResponse result = client.Execute(request);
            //return Newtonsoft.Json.JsonConvert.DeserializeObject<RoutItem>(result.Content);
        }
    }
}
